package com.zis.auction.service.impl;

import com.zis.auction.dao.CounselDao;
import com.zis.auction.model.Counsel;
import com.zis.auction.service.ICounselInter;
import com.zis.base.impl.BaseInterImpl;
import com.zis.core.Constant;
import com.zis.core.ResponseEntity;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2018/1/12
 */
@Service
@Transactional("txManager")
public class CounselInterImpl extends BaseInterImpl<Counsel> implements ICounselInter {

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private CounselDao counselDao;

    @Override
    public String insert(Counsel counsel) throws Exception {
        counsel.setConTime(new Date());
        counsel.setState(0);
        int num = sqlManager.insert(counsel);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String update(Counsel counsel) throws Exception {
        if(!counsel.getReplyUser().equals("")&&!counsel.getReplyContent().equals("")){
            counsel.setState(1);
            counsel.setReplyTime(new Date());
        }
        int num = sqlManager.updateTemplateById(counsel);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String delete(String id) throws Exception {
        int num = sqlManager.deleteById(Counsel.class, id);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public Counsel selectById(String id) throws Exception {
        return counselDao.findById(id);
    }

    @Override
    public List<Counsel> findByCondition(Counsel counsel) throws Exception {
        return sqlManager.template(counsel);
    }

    @Override
    public List<Counsel> findByAusId(String ausId) throws Exception {
        return counselDao.findByAusId(ausId);
    }
}
